package com.bibrApp.difu.actvity.notice_center_mvp;

import android.content.Intent;
import android.view.View;

import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.NoticeResBody;
import com.bibrApp.difu.actvity.notice_detail_mvp.NoticeDetailActivity;
import com.bibrApp.difu.adapter.NoticeCenterAdapter;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.widget.HorizontalDividerItemDecoration;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class NoticeCenterActivity extends BaseActivity<NoticeCenterPresenter> implements INoticeCenterView {

    @BindView(R.id.return_iv)
    View return_iv;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    ImmersionBar mImmersionBar;
    private NoticeCenterAdapter mAdapter;
    private List<NoticeResBody.ListBean> mDatas = new ArrayList<>();

    private int mPageNum = 1;
    private static final int PAGE_SIZE = 20;

    @Override
    protected void initPresenter() {
        presenter = new NoticeCenterPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notice_center;
    }

    @Override
    public void initView() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColor(R.color.white)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();


        mAdapter = new NoticeCenterAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .marginResId(R.dimen.dp_12, R.dimen.dp_12)
                .color(getResources().getColor(R.color.black_f0))
                .build());
        recyclerView.setAdapter(mAdapter);

        refreshLayout.autoRefresh();
        setOnClick();
    }

    public void loadData(boolean isRefresh) {
        presenter.getNoticeCenter(isRefresh, mPageNum, PAGE_SIZE);
    }

    private void setOnClick() {
        return_iv.setOnClickListener(v -> finish());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            NoticeResBody.ListBean data = (NoticeResBody.ListBean) adapter.getItem(position);
            Intent intent = new Intent(this, NoticeDetailActivity.class);
            intent.putExtra("id", data.getId());
            intent.putExtra("type", 1);
            startActivity(intent);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> loadData(false));
        refreshLayout.setOnRefreshListener(refreshLayout -> loadData(true));
    }


    @Override
    public void successOfNotice(NoticeResBody data, boolean isRefresh) {
        NoticeResBody dataBean = data;
        if (dataBean.isHasNextPage()) {
            mPageNum = dataBean.getNextPage();
            refreshLayout.setEnableLoadMore(true);
        } else {
            refreshLayout.setEnableLoadMore(false);
        }

        if (isRefresh) {
            mDatas.clear();
        }
        mDatas.addAll(dataBean.getList());
        mAdapter.setNewData(mDatas);
    }

    @Override
    public void endRefresh(boolean isRefresh) {
        if (isRefresh) {
            refreshLayout.finishRefresh();
        } else {
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }
}
